package com.primaryedu.ui.app.messageboard;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.primaryedu.R;
import com.primaryedu.bean.MessagesBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/9/12.
 */

public class MessagesListAdapter extends BaseAdapter {

    private final String type;
    private Context context;
    LayoutInflater inflater;
    private List<MessagesBean.DataBean.RowsBean> list = new ArrayList<>();

    public MessagesListAdapter(Context context, List<MessagesBean.DataBean.RowsBean> list, String type) {
        this.context = context;
        this.list = list;
        this.type = type;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void updateView(List<MessagesBean.DataBean.RowsBean> nowList) {
        this.list = nowList;
        this.notifyDataSetChanged();//强制动态刷新数据进而调用getView方法
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MessagesListAdapter.ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new MessagesListAdapter.ViewHolder();
            convertView = inflater.inflate(R.layout.item_messageslist, null);
            //姓名
            viewHolder.tv_nameclass = (TextView) convertView.findViewById(R.id.tv_nameclass);
            //时间
            viewHolder.tv_data = (TextView) convertView.findViewById(R.id.tv_data);
            //留言
            viewHolder.tv_message = (TextView) convertView.findViewById(R.id.tv_message);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MessagesListAdapter.ViewHolder) convertView.getTag();
        }

        MessagesBean.DataBean.RowsBean mr = list.get(position);
        viewHolder.tv_nameclass.setText(mr.getStudentName());
        viewHolder.tv_data.setText(mr.getCreateTime());
        viewHolder.tv_message.setText(mr.getContent());
        return convertView;
    }

    public void addAll(List<MessagesBean.DataBean.RowsBean> leaveLists) {
        list.addAll(leaveLists);
        notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tv_message;
        TextView tv_data;
        TextView tv_nameclass;
    }
}
